/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * ScriptDisplayAndEditor.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class ScriptDisplayAndEditor extends JFrame implements ActionListener {
    private List<String> scriptLines;

    private JTextArea scriptEditorArea;
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenuItem saveAsSubMenu;
    private JScrollPane scrollPane;

    public ScriptDisplayAndEditor(List data) {
	super();
	scriptLines = data;
	this.setSize(600, 800);
	this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	initComponents();
	addComponents();
    }

    /**
     * 
     */
    private void initComponents() {
	scriptEditorArea = new JTextArea(200, 50);
	menuBar = new JMenuBar();
	fileMenu = new JMenu("File");
	saveAsSubMenu = new JMenuItem("Save As");
	scrollPane = new JScrollPane(scriptEditorArea);

	scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

	// action listeners
	scriptEditorArea.setPreferredSize(new Dimension(580, 780));
	saveAsSubMenu.addActionListener(this);

    }

    /**
     * 
     */
    private void addComponents() {
	this.add(scrollPane, BorderLayout.CENTER);
	this.setJMenuBar(menuBar);

	menuBar.add(fileMenu);
	fileMenu.add(saveAsSubMenu);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
	// TODO Auto-generated method stub

    }

    /**
     * Displays script in display area
     * */
    public void showScript(SegmentConfigurationPanelHandler segmentConfigurationPanelHandler) {
	addStudyInformation(segmentConfigurationPanelHandler);
	addExperimentMode();
	addFontsData();
	addExperimentalParameters();
	addSegmentsData();

	this.setVisible(true);

    }

    /**
     * 
     */
    private void addStudyInformation(SegmentConfigurationPanelHandler panel) {
	// TODO Auto-generated method stub
	String studyName = panel.getStudyName();
	String binaryDecisionTask = panel.getBinaryDecisionTask();
	if (studyName == null || studyName.equals("")) {
	    // append nothing
	} else {
	    scriptEditorArea.append("%\n");
	    scriptEditorArea.append("%Study Name:\t" + studyName + "\n");
	    scriptEditorArea.append("%\n");
	}

	if (binaryDecisionTask == null || binaryDecisionTask.equals("")) {
	    // append nothing
	} else {
	    scriptEditorArea.append("%\n");
	    scriptEditorArea.append("%Binary Decision Task:\t" + binaryDecisionTask + "\n");
	    scriptEditorArea.append("%\n");
	}

    }

    /**
     * 
     */
    private void addExperimentalParameters() {
	String header = "#experimental parameters\n#\n";
	ExperimentalParametersPanelHandler experimentalParametersPanelHandler = ExperimentalParametersPanelHandler
		.getInstance();
	String experimentalParameters = experimentalParametersPanelHandler.getExperimentalParametersString();
	scriptEditorArea.append(header);
	scriptEditorArea.append(experimentalParameters);
	scriptEditorArea.append("\n#\n#\n");

    }

    /**
     * 
     */
    private void addExperimentMode() {
	String header = "#\n# Experiment Mode:\n#\t1 - Unimodal 1: reaction time for each probe\n#\t2 - Unimodal 2: reaction time every other probe\n#\t3 - Cross Modal\n#\t4 - Cross Modal ERP\n#\t5 - Cross Modal IMG\n#\t6 - Unimodal Word by Word\n#\t7 - Cross Modal HEMI-FIELD\n#\t8 - Unimodal chunk by chunk with ERP\n#\t9 - Video\n#\t10 - Video with ERP\n#\t11 - Cross modal with functional-MRI\n#\n#\n";
	String mode = Integer.toString(ModeSelectorScreenHandler.getMode());
	scriptEditorArea.append(header);
	scriptEditorArea.append(mode);
	scriptEditorArea.append("\n#\n#\n");

    }

    /**
     * 
     */
    private void addFontsData() {
	FontParametersPanelHandler fontParametersPanelHandler = FontParametersPanelHandler.getInstance();
	String fontParameters = fontParametersPanelHandler.getFontParametersString();
	scriptEditorArea.append("# Font parameters\n");
	scriptEditorArea.append(fontParameters + "\n");
	scriptEditorArea.append("#\n");

    }

    /**
     * 
     */
    private void addSegmentsData() {
	for (String listItem : scriptLines) {
	    scriptEditorArea.append(listItem);
	}
    }

}
